/**
 * 类名：DownloadAction
 * 创建日期： 2014-11-18 下午11:15:07
 * 
 */
package com.erms.action;

import java.io.InputStream;

import javax.annotation.Resource;

import com.erms.bean.Courseware;
import com.erms.bean.FileInfo;
import com.erms.domain.CoursewareService;
import com.erms.domain.FileService;
import com.erms.domain.HomeworkService;
import com.erms.domain.MessageService;
import com.erms.domain.TaskService;
import com.erms.util.StringUtil;

/**
 * @author hmb
 * 备注： 
 *
 */
public class DownloadAction extends BaseAction {

	private static final long	serialVersionUID	= -1617465906658980021L;
	@Resource
	private FileService fileService;
	
	
	private FileInfo fileInfo;
	
	private InputStream targetInputStream;
	
	private String newFileName;
		
	private Integer fileID;

	public Integer getFileID() {

		return fileID;
	}

	public void setFileID(Integer fileID) {

		this.fileID = fileID;
	}
	
	/**
	 * @see com.erms.action.BaseAction#execute()
	 * @return
	 * @throws Exception
	 */
	@Override
	public String execute() throws Exception {
	
		// TODO 自动生成的方法存根
		String username = (String)getSession().getAttribute(KEY_USERNAME);
		if (StringUtil.isNull(username)) {
			return LOGIN;
		}
		logger.debug("下载文件，id=" + fileID);
		try {
			this.targetInputStream = fileService.download(fileID);
			fileInfo = fileService.find(fileID);
			String name = fileInfo.getName();
			int a= name.lastIndexOf(".");
			int b = name.lastIndexOf("_");
			if (a > 0 && b > 0) {
				newFileName = name.substring(0, b)+name.substring(a);
			}else {
				newFileName = name;
			}
			logger.debug("服务器文件名："+name+",下载文件名:"+newFileName);
			logger.debug("文件类型:" + fileInfo.getType());
			newFileName = StringUtil.urlEncode(newFileName);
		} catch (Exception e) {
			// TODO: handle exception
			logger.error("下载异常",e);
			
		}
		/*if (targetInputStream == null) {
			return null;
		}*/
		return SUCCESS;
	}
	
	
	public InputStream getTargetInputStream() {

		return targetInputStream;
	}

	public String getNewFileName() {

		return newFileName;
	}

	public void setNewFileName(String newFileName) {

		this.newFileName = newFileName;
	}

	public FileInfo getFileInfo() {

		return fileInfo;
	}

	public void setFileInfo(FileInfo fileInfo) {

		this.fileInfo = fileInfo;
	}


}
